﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using CounterServerModel;


namespace SmartCounters
{
	public partial class BillsReportrmation : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            btShowChart.Visible = false;
            if (!this.IsPostBack)
            {
                ddAcounts.Items.Clear();
                ddAcounts.Items.Add(new ListItem("--Please Select Acount--", "-1"));
                ddFromDate.Items.Clear();
                ddToDate.Items.Clear();

                List<AccountInfo> accounts = new List<AccountInfo>();
                ((MainTemplate)Master).m_PagePermissionFilter = MainTemplate.k_UserPageFilter;
                InformationReader reader = new InformationReader();
                UserInfo userInfo = reader.GetUserInformation(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, ((MainTemplate)Master).AuthenticatedLoggedInUserCredentials.ID);
                if ((userInfo.UserPermissions & eUserPermissions.ReadOtherBill) == eUserPermissions.ReadOtherBill)
                {
                    accounts = reader.GetAllAccounts(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials);
                }
                else if ((userInfo.UserPermissions & eUserPermissions.ReadOwnBill) == eUserPermissions.ReadOwnBill)
                {
                    foreach (int accountID in userInfo.AccountsAssociatedWith)
                    {
                        accounts.Add(reader.GetAccountInformation(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, accountID));
                    }
                }

                foreach (AccountInfo accountInfo in accounts)
                {
                    string accountLable = accountInfo.Country + " " + accountInfo.City + " " + accountInfo.Street + " " + accountInfo.House + "/" + accountInfo.Apartment;
                   
                    ddAcounts.Items.Add(new ListItem(accountLable, accountInfo.AccountID.ToString()));
                }

                if (ddAcounts.Items.Count > 0)
                {
                    ddAcounts.SelectedIndex = 0;
                }
            }
		}

        protected void btShowAllUsages_Click(object sender, EventArgs e)
        {
            chart.Visible = false;
            usagesGrid.Visible = true;
            
            int accountID = int.Parse(ddAcounts.SelectedItem.Value);
            if (!(ddFromDate.SelectedItem == null || ddToDate.SelectedItem == null))
            {
                int fromYear = int.Parse(ddFromDate.SelectedItem.Value.Substring(0, 4));
                int fromMonth = int.Parse(ddFromDate.SelectedItem.Value.Substring(5, 2));
                int toYear = int.Parse(ddToDate.SelectedItem.Value.Substring(0, 4));
                int toMonth = int.Parse(ddToDate.SelectedItem.Value.Substring(5, 2));
                BillingService billingService = new BillingService();
                DataTable acountBills = billingService.GetBillsTablePerAcount(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, accountID, fromYear, fromMonth, toYear, toMonth);
                usagesGrid.DataSource = acountBills;
                usagesGrid.DataBind();
            }
            else
            {
                //do we pop up message for errors
            }
        }

        protected void btShowChart_Click(object sender, EventArgs e)
        {
        }

        protected void ddAcounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedAccount = Convert.ToInt32(((System.Web.UI.WebControls.ListControl)(sender)).SelectedItem.Value);
            DataTable acountBills;
            string currYYYYMM = "";
            BillingService billingService= new BillingService();
            acountBills = billingService.GetBillsTablePerAcount(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials,selectedAccount);
            ddFromDate.Items.Clear();
            ddToDate.Items.Clear();
            foreach (DataRow acountBill in acountBills.Rows)
            {
              currYYYYMM = Convert.ToString(acountBill[0]) + "-" + Convert.ToString(acountBill[1]);
              ddFromDate.Items.Add(new ListItem(currYYYYMM, currYYYYMM+"  "));
              ddToDate.Items.Add(new ListItem(currYYYYMM, currYYYYMM+ "  "));
            }

       }

        protected void usagesGrid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
	}
}